//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Research.DataLayer;

namespace Microsoft.Research.DataLayer
{
    /// <summary>
    /// Shows progress when loading from the data store
    /// </summary>
    public partial class SaveToStoreProgress : Form
    {
        private Form parent;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parent"></param>
        public SaveToStoreProgress(Form parent)
        {
            this.parent = parent;

            InitializeComponent();
        }

        /// <summary>
        /// Handler to bind and receive notifications about transfers
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="owner"></param>
        /// <param name="obj"></param>
        /// <param name="field"></param>
        /// <param name="curChunk"></param>
        /// <param name="maxChunks"></param>
        /// <param name="curChunkLen"></param>
        /// <param name="chunkSize"></param>
        /// <param name="conn"></param>
        public void TransferProgressHandler(BlobBase.TransferDirection dir, object owner, object obj, string field,
            long curChunk, long maxChunks, long curChunkLen, long chunkSize, Connection conn)
        {
            if (conn is IConnectionProviderInfo)
            {
                connLogo.Image = ((IConnectionProviderInfo)conn).IconSmall;
            }

            itemAction.Text = string.Format("Saving item: {0}...", field);
            itemProgress.Maximum = (int)maxChunks;
            itemProgress.Value = (int)curChunk;
            itemPicture.Visible = (obj is Image);

            if (obj is Image)
            {
                itemPicture.Image = (Image)obj;
            }

            Application.DoEvents();
        }

        /// <summary>
        /// Handler to bind and receive notifications about transfers
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="field"></param>
        /// <param name="conn"></param>
        public void BeginClearStoreProgressHandler(object owner, string field, Connection conn)
        {
            if (conn is IConnectionProviderInfo)
            {
                connLogo.Image = ((IConnectionProviderInfo)conn).IconSmall;
            }

            itemAction.Text = string.Format("Action: Clearing old data for '{0}'", field);
            Application.DoEvents();
        }

        /// <summary>
        /// Handler to bind and receive notifications about transfers
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="field"></param>
        /// <param name="conn"></param>
        public void EndClearStoreProgressHandler(object owner, string field, Connection conn)
        {
            if (conn is IConnectionProviderInfo)
            {
                connLogo.Image = ((IConnectionProviderInfo)conn).IconSmall;
            }

            itemAction.Text = string.Format("Action: Done clearing old data for '{0}'", field);
            Application.DoEvents();
        }

        private void SaveToStoreProgress_Shown(object sender, EventArgs e)
        {
        }

        private void SaveToStoreProgress_Load(object sender, EventArgs e)
        {
            if (parent != null)
            {
                this.Left = parent.Left + (parent.Width - this.Width) / 2;
                this.Top = parent.Top + (parent.Height - this.Height) / 2;
            }

            itemAction.Text = "Initializing...";
            itemPicture.Visible = false;

            Application.DoEvents();
        }

        private void SaveToStoreProgress_Paint(object sender, PaintEventArgs e)
        {
            Application.DoEvents();
        }

        private void SaveToStoreProgress_Activated(object sender, EventArgs e)
        {
            Application.DoEvents();
        }
    }
}
